// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()
const db = cloud.database()
// 云函数入口函数
exports.main = async (event, context) => {
	const wxContext = cloud.getWXContext()
	var mywordid = event.word_id
	var myopenid = wxContext.OPENID
	console.log("In Function[removeWrong] event: ", event)
	const countResult = await db.collection('wrongNote').where({
		word_id: mywordid,
		user_id: myopenid
	}).count()
	const num = countResult.total
	if (num != 0) {
		const tasks = []
		const promise = db.collection('wrongNote').where({
			word_id: mywordid,
			user_id: myopenid
		}).get()
		tasks.push(promise)
		const deleteidinfo = (await Promise.all(tasks)).reduce((acc, cur) => {
			return {
				data: acc.data.concat(cur.data),
				errMsg: acc.errMsg,
			}
		})
		await db.collection('wrongNote').doc(deleteidinfo.data[0]._id).remove()
		return {
			msg: 'ok'
		}
	} else
		return {
			msg: 'error'
		}
}